if(length(tryCatch(list.dirs(path="~/Library/CloudStorage/Dropbox/Robots and Trade in Services"),error=function(e) e))==0){
  base.path<-"~/Dropbox/Robots and Trade in Services/"
} else {
  base.path<-"~/Library/CloudStorage/Dropbox/Robots and Trade in Services/"
}

#Load data and functions
source(paste0(base.path,"Drafts/Replication/03 Functions and Processing.R"))

lucid.proc$policy_difference <- ifelse(lucid.proc$treatment_automation_labor=="Labor",lucid.proc$restrict_imports_difference,lucid.proc$restrict_automation_difference)

lucid.proc$treatment_fdal_dabase = relevel(lucid.proc$treatment_fdal, ref=2)

##########################
## Table 6 (Appendix B) ##
##########################

library(stargazer)
library(estimatr)

controls_vec<-balance_controls_vec

lucid.proc <- lucid.proc %>%   mutate(
  benefits_to_unemployed_share3l=benefits_to_unemployed/(restrict_imports+benefits_to_unemployed),
  benefits_to_unemployed_share3a=benefits_to_unemployed/(restrict_automation+benefits_to_unemployed),
  restrict_imports_share3=restrict_imports/(restrict_imports+benefits_to_unemployed),
  restrict_automation_share3=restrict_automation/(restrict_automation+benefits_to_unemployed))

lucid.proc$policy_share3 <- ifelse(lucid.proc$treatment_automation_labor=="Labor",lucid.proc$restrict_imports_share3,lucid.proc$restrict_automation_share3)

lucid.proc$benefits_to_unemployed_share3 <- ifelse(lucid.proc$treatment_automation_labor=="Labor",lucid.proc$benefits_to_unemployed_share3l,lucid.proc$benefits_to_unemployed_share3a)


m1a <- lm(policy_share3 ~ treatment_fdal_dabase + sample_date, data=subset(lucid.proc, (treatment_fdal_dabase == "Domestic Automation" | treatment_fdal_dabase == "Foreign Labor")))
m1b <- lm(benefits_to_unemployed_share3 ~ treatment_fdal_dabase + sample_date, data=subset(lucid.proc, (treatment_fdal_dabase == "Domestic Automation" | treatment_fdal_dabase == "Foreign Labor")))

m2a <- lm(paste0("policy_share3 ~ treatment_fdal_dabase + sample_date +",paste(controls_vec,collapse=" + ")),data=subset(lucid.proc, treatment_fdal_dabase == "Domestic Automation" | treatment_fdal_dabase == "Foreign Labor"))
m2b <- lm(paste0("benefits_to_unemployed_share3 ~ treatment_fdal_dabase + sample_date +",paste(controls_vec,collapse=" + ")),data=subset(lucid.proc, treatment_fdal_dabase == "Domestic Automation" | treatment_fdal_dabase == "Foreign Labor"))

m3a <- lm(restrict_automation_share3 ~ treatment_fdal_dabase + sample_date, data=subset(lucid.proc, treatment_fdal_dabase == "Domestic Automation" | treatment_fdal_dabase == "Foreign Automation"))
m3b <- lm(benefits_to_unemployed_share3 ~ treatment_fdal_dabase + sample_date, data=subset(lucid.proc, treatment_fdal_dabase == "Domestic Automation" | treatment_fdal_dabase == "Foreign Automation"))

m4a <- lm(paste0("restrict_automation_share3 ~ treatment_fdal_dabase + sample_date +",paste(controls_vec,collapse=" + ")),data=subset(lucid.proc, treatment_fdal_dabase == "Domestic Automation" | treatment_fdal_dabase == "Foreign Automation"))
m4b <- lm(paste0("benefits_to_unemployed_share3 ~ treatment_fdal_dabase + sample_date +",paste(controls_vec,collapse=" + ")),data=subset(lucid.proc, treatment_fdal_dabase == "Domestic Automation" | treatment_fdal_dabase == "Foreign Automation"))

stargazer(m1a,m2a,m3a,m4a, se = starprep(m1a,m2a,m3a,m4a),
          keep.stat="n", header = FALSE,
          title="Effect of Treatment on Policy Shares",
          label = "tab:mainsharesver2",
          omit.table.layout ="dl",
          keep = c("treatment_fdal_dabaseForeign Automation","treatment_fdal_dabaseForeign Labor", "Constant"),
          covariate.labels=c("For. Labor","For. Auto."),
          add.lines=list(c("Controls?", "N","Y","N","Y")), no.space=TRUE)
 